<template>
  <div class="photographers-container">
    <el-card class="photographers-card">
      <!-- 摄影师列表 -->
      <el-row :gutter="20">
        <el-col :span="8" v-for="photographer in photographers" :key="photographer.id">
          <el-card class="photographer-card" :body-style="{ padding: '0px' }">
            <div class="image-wrapper">
              <img :src="photographer.avatar" class="photographer-avatar" />
            </div>
            <div class="photographer-info">
              <div>
                <h3>
                  {{ photographer.nickname }}
                  <span :class="['level-tag', `level-${photographer.level}`]">
                    {{ getLevel(photographer.level) }}
                  </span>
                </h3>
              </div>
              <p class="description">{{ photographer.introduction }}</p>
              <div class="stats">
                <span>作品数: {{ photographer.samples_count }}</span>
                <span>评分: {{ photographer.rating }}</span>
              </div>
              <div class="price">￥{{ photographer.price_range.min }}起</div>
              <div class="actions">
                <el-button type="primary" size="small" @click="handleViewPortfolio(photographer)">
                  查看作品集
                </el-button>
                <!-- <el-button
                  v-if="userRole === 'user'"
                  type="success"
                  size="small"
                  @click="handleBook(photographer)"
                >
                  立即预约
                </el-button> -->
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>

      <!-- 分页 -->
      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :total="total"
          :page-sizes="[9, 18, 36]"
          layout="total, sizes, prev, pager, next"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted, computed } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { useUserStore } from '@/stores/user'
import Logger from '@/utils/logger'
import type { Photographer } from '@/types/photographer'
import { getPhotographersList } from '@/api/photographer'

const router = useRouter()
const userStore = useUserStore()
const userRole = computed(() => userStore.userRole)

// 分页相关
const currentPage = ref(1)
const pageSize = ref(9)
const total = ref(0)

// 模拟摄影师数据
const photographers = ref<Photographer[]>([])

// 获取摄影师等级
const getLevel = (level: number) => {
  const labels: Record<number, string> = {
    1: '初级摄影师',
    2: '高级摄影师',
    3: '资深摄影师',
    4: '首席摄影师',
  }
  return labels[level]
}

// 处理分页
const handleSizeChange = (val: number) => {
  pageSize.value = val
  fetchData()
}

const handleCurrentChange = (val: number) => {
  currentPage.value = val
  fetchData()
}

// 获取摄影师列表
const fetchData = async () => {
  try {
    const { data } = await getPhotographersList({
      page: currentPage.value,
      pageSize: pageSize.value,
    })
    photographers.value = data.data.items
    total.value = data.data.meta.total
  } catch (error) {
    ElMessage.error('获取数据失败')
  }
}

// 查看作品集
const handleViewPortfolio = (photographer: Photographer) => {
  router.push(`/portfolio/${photographer.id}`)
}

// 预约摄影师
const handleBook = (photographer: Photographer) => {
  if (!userStore.isLoggedIn) {
    ElMessage.warning('请先登录后再预约')
    router.push('/login')
    return
  }

  if (userStore.userRole != 'user') {
    ElMessage.warning('只有用户可以预约')
    return
  }

  router.push({
    path: '/booking',
    query: { photographer_id: photographer.id },
  })
}

onMounted(() => {
  fetchData()
})
</script>

<style scoped>
.photographers-container {
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
  min-height: calc(100vh - 200px);
  box-sizing: border-box;
}

.photographers-card {
  margin-top: 0;
  flex: 1;
}

.filter-section {
  margin-bottom: 20px;
}

.photographer-card {
  margin-bottom: 20px;
  transition: transform 0.3s;
}

.photographer-card:hover {
  transform: translateY(-5px);
}

.image-wrapper {
  width: 100%;
  padding-top: 98%;
  position: relative;
  overflow: hidden;
}

.photographer-avatar {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s ease;
}

.photographer-card:hover .photographer-avatar {
  transform: scale(1.05);
}

.photographer-info {
  padding: 15px;
}

.photographer-info h3 {
  margin: 0;
  font-size: 18px;
  color: #333;
}

.type {
  color: #666;
  font-size: 14px;
  margin: 5px 0;
}

.description {
  color: #666;
  font-size: 14px;
  margin: 10px 0;
  height: 40px;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.stats {
  display: flex;
  justify-content: space-between;
  color: #666;
  font-size: 14px;
  margin: 10px 0;
}

.price {
  color: #f56c6c;
  font-size: 16px;
  font-weight: bold;
  margin: 10px 0;
}

.actions {
  display: flex;
  justify-content: space-between;
  margin-top: 15px;
}

.pagination {
  margin-top: 20px;
  text-align: center;
}

.level-tag {
  display: inline-block;
  font-size: 12px;
  padding: 2px 6px;
  border-radius: 4px;
  margin-left: 8px;
  font-weight: normal;
}

.level-1 {
  background-color: #e1f3d8;
  color: #67c23a;
}

.level-2 {
  background-color: #fdf6ec;
  color: #e6a23c;
}

.level-3 {
  background-color: #f0f9ff;
  color: #409eff;
}

.level-4 {
  background-color: #fef0f0;
  color: #f56c6c;
}
</style>
